package cn.tedu;

import java.sql.*;
import java.util.Scanner;

public class Demo10 {
    //登录,输入用户名,密码
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String name = sc.nextLine();
        System.out.println("请输入密码:");
        String password = sc.nextLine();
        try(Connection conn = DBUtils.getConn()) {
            //sql骨架
            String sql = "select count(*) from user where username = ? and password = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            /**
             * 代码中1和2代表的就是?的位置,到时候会将变量把?替换掉
             * sql代码逻辑已经编译,提前锁死,现在用户输入的内容则不会影响原有的sql语句逻辑
             */
            ps.setString(1,name);
            ps.setString(2,password);
            ResultSet rs = ps.executeQuery();
            rs.next();
            if(rs.getInt(1) > 0) {
                System.out.println("登录成功!");
            } else {
                System.out.println("登陆失败!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
